package com.example.Lezedata.ui.fragment.home;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.example.Lezedata.R;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.fragment.BaseFragment;
import com.example.Lezedata.util.GBInterface;
import com.zhpan.bannerview.BannerViewPager;
import com.zhpan.bannerview.BaseBannerAdapter;
import com.zhpan.bannerview.BaseViewHolder;
import com.zhpan.bannerview.constants.IndicatorGravity;
import com.zhpan.bannerview.utils.BannerUtils;

import java.util.ArrayList;
import java.util.List;

import api.BidServiceGrpc;
import bid.Bid;
import common.Common;

public class HomeBanner extends BaseFragment {

    List<Bid.Banner> list;
    public GBInterface viewCreatedListener;
    BannerAdapter adapter;
    BannerViewPager viewPager;
    public interface OnTapItemListener {
        void onTapItem(Bid.Banner data);
    };
    public OnTapItemListener itemListener;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.home_bannerview, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ViewGroup view = (ViewGroup)contentView.getParent();
        if (view != null){
            view.setClipToPadding(false);
            view.setClipChildren(false);
        }
    }

    @Override
    public void initView(){
        super.initView();

        if (viewCreatedListener != null){
            viewCreatedListener.onViewCreated(contentView);
        }
        adapter = BannerAdapter.newInstance(getContext());
        viewPager = (BannerViewPager)this.getView().findViewById(R.id.BannerViewPager);
        viewPager.setIndicatorSliderGap(BannerUtils.dp2px(6))
                .setScrollDuration(800)
                .setLifecycleRegistry(getLifecycle())
                .setIndicatorGravity(IndicatorGravity.CENTER)
                .setIndicatorSliderColor(Color.WHITE, getResources().getColor(R.color.app_color_main))
                .setOnPageClickListener(new BannerViewPager.OnPageClickListener() {
                    @Override
                    public void onPageClick(View clickedView, int position) {
                        tapPosition(position);
                    }
                }).setAdapter(adapter).create();
    }

    public void reloadData(Bid.BannerListResponse value){
        list = value.getListList();
        viewPager.refreshData(list);
    }

    void tapPosition(int position){
        if (itemListener != null){
            itemListener.onTapItem(list.get(position));
        }
        Log.d(TAG, "tapPosition:" + position);
    }
}

class BannerAdapter extends BaseBannerAdapter<Bid.Banner> {
    Context context;

    public static BannerAdapter newInstance(Context context) {
        BannerAdapter fragment = new BannerAdapter();
        fragment.context = context;
        return fragment;
    }

    @Override
    protected void bindData(BaseViewHolder<Bid.Banner> holder, Bid.Banner data, int position, int pageSize) {
        ImageView imageView = holder.findViewById(R.id.banner_content);
        Glide.with(context).load(data.getBannerUrl()).placeholder(R.drawable.login_logo).into(imageView);
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.home_banner_imageview;
    }
}